package com.fary.expression;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Fary
 * @version 1.0
 * @description: TODO
 * @date 2021/11/9 9:00
 */
public class TestExpression {

    public static void main(String[] args) {
        String spelExpression = "#diffDead/#average * #score";

        Map<String, Object> variables = new HashMap<>();
        variables.put("diffDead", 25);
        variables.put("average", 21d);
        variables.put("score", 100);

        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariables(variables);
        Expression exp = parser.parseExpression(spelExpression);
        Integer value = exp.getValue(context, Integer.class);
        System.out.println("value：" + value);
    }
}
